package com.jinlejia.erp.widget.image;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinlejia.erp.R;
import com.utils.PathToUriToFileUtils;

public class UploadImageView extends RelativeLayout{

    private Context context;

    public UploadImageView(Context context) {
        this(context, null);
    }

    public UploadImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UploadImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public UploadImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }
    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }
    private ImageBean imageBean;
    private RelativeLayout reloadLayout;
    private TextView reloadTextView;
    private ImageView showImageView;
    private ImageView closeImage;
    private ImageView noteImageView;
    private void init(Context context){
        this.context = context;
        imageBean = new ImageBean();
        View view = View.inflate(context, R.layout.weight_upload_image_view, this);
        reloadLayout = view.findViewById(R.id.reloadLayout);
        reloadLayout.setVisibility(View.GONE);
        reloadTextView = view.findViewById(R.id.reloadTextView);
        reloadTextView.setVisibility(View.GONE);
        showImageView = view.findViewById(R.id.showImageView);
        showImageView.setVisibility(View.GONE);
        noteImageView = view.findViewById(R.id.noteImageView);
        noteImageView.setVisibility(View.VISIBLE);
        closeImage = view.findViewById(R.id.closeImage);
        closeImage.setVisibility(View.GONE);
        reloadTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                imageBean.state = 1; //标识上传中
                reloadTextView.setVisibility(View.GONE);
                listener.onUploadImage(UploadImageView.this,imageBean.originalUrl);
            }
        });
        closeImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                imageBean.serverUrl = null;
                imageBean.originalUrl = null;
                imageBean.state = 0;
                reloadLayout.setVisibility(View.GONE);
                closeImage.setVisibility(View.GONE);
                noteImageView.setVisibility(View.VISIBLE);
                showImageView.setImageURI(null);
                showImageView.setVisibility(View.GONE);
                listener.onDeleteImage(UploadImageView.this);
            }
        });
        noteImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onChooseImage(UploadImageView.this);
            }
        });
        showImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageBean.originalUrl==null){ //如果没有选择图片时，则进行选择图片
                    listener.onChooseImage(UploadImageView.this);
                }else { //如果有图片时，预览图片
                    if (imageBean.serverUrl!=null){
                        listener.onPreviewImage(UploadImageView.this,imageBean.serverUrl);
                    }else {
                        listener.onPreviewImage(UploadImageView.this,imageBean.originalUrl);
                    }
                }
            }
        });
    }

    /**
     * 获取服务器地址
     */
    public String getServerUrl() {
        return imageBean.serverUrl;
    }


    /**
     * 删除所有图片信息
     */
    public void deleUrlMessage() {
        imageBean.serverUrl = null;
        imageBean.originalUrl = null;
        imageBean.state = 0;
        reloadLayout.setVisibility(View.GONE);
        closeImage.setVisibility(View.GONE);
        noteImageView.setVisibility(View.VISIBLE);
        showImageView.setImageURI(null);
        showImageView.setVisibility(View.GONE);
    }

    public String getOriginalUrl(){
        return imageBean.originalUrl;
    }

    /**
     *
     * @return true:没有选择图片 false:其他
     */
    public boolean isEmpty(){
        return TextUtils.isEmpty(imageBean.originalUrl);
    }

    /**
     *
     * @return true:图片上传成功 false:其他
     */
    public boolean isSuccessBean(){
        return imageBean.state == 2 && !TextUtils.isEmpty(imageBean.serverUrl);
    }

    /**
     * 上传成功
     * @param serverUrl 服务器返回的地址
     */
    public void uploadSuccess(String serverUrl){
        imageBean.serverUrl = serverUrl;
        imageBean.state = 2;
        reloadLayout.setVisibility(View.GONE);
        reloadTextView.setVisibility(View.GONE);
    }

    /**
     * 上传失败
     */
    public void uploadFailed(){
        imageBean.serverUrl = null;
        imageBean.state = -1;
        reloadLayout.setVisibility(View.VISIBLE);
        reloadTextView.setVisibility(View.VISIBLE);
    }

    /**
     * 选择图片成功
     * @param originalUrl 本地图片地址
     */
    public void selectSuccess(String originalUrl){
        imageBean.originalUrl = originalUrl;
        Uri imageContentUri = PathToUriToFileUtils.getImageContentUri(context, originalUrl);
//        showImageView.setImageURI(Uri.parse(originalUrl));
        showImageView.setVisibility(View.VISIBLE);
        Glide.with(context).load(imageContentUri).placeholder(R.mipmap.ic_no_img).into(showImageView);
        reloadTextView.setVisibility(View.GONE);
        reloadLayout.setVisibility(View.VISIBLE);
        noteImageView.setVisibility(View.GONE);
        closeImage.setVisibility(View.VISIBLE);
        imageBean.state = 1; //标识上传中
        listener.onUploadImage(this,originalUrl);
    }

    int getState(){
        return imageBean.state;
    }

    public interface Listener{
        void onDeleteImage(UploadImageView view);
        void onChooseImage(UploadImageView view);
        void onPreviewImage(UploadImageView view, String previewUrl);
        void onUploadImage(UploadImageView view, String originalUrl);
    }

    private final static class ImageBean{
        private String originalUrl;
        private String serverUrl;
        private int state; //-1:上传失败 0:未选择 1:上传中 2:上传完成

        public String getOriginalUrl() {
            return originalUrl;
        }

        private String getServerUrl() {
            return serverUrl;
        }

        public int getState() {
            return state;
        }
    }
}
